<template>
  <div class="app-container">
    <el-card style="margin-bottom: 10px">
      <el-row :gutter="24" style="margin-bottom: -15px">
        <el-col :span="24" :xs="24">
          <el-form
            :model="queryParams"
            ref="queryForm"
            :inline="true"
            v-show="showSearch"
            label-width="100px"
          >
            <el-form-item label="合同所属公司" prop="companyName">
              <el-select
                v-model="queryParams.belongingCompanyId"
                placeholder="请选择"
                clearable
                @change="handleQuery"
              >
                <el-option
                  v-for="item in selectorParameter"
                  :key="item.belongingCompanyId"
                  :label="item.name"
                  :value="item.belongingCompanyId"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="原件归档位置" prop="archiveLocation">
              <el-input
                v-model="queryParams.archiveLocation"
                placeholder="请输入"
                clearable
                style="width: 240px"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item style="float: right; margin-left: auto">
              <el-button
                type="primary"
                icon="el-icon-search"
                @click="handleQuery"
                >搜索</el-button
              >
              <el-button icon="el-icon-refresh" @click="resetQuery"
                >重置</el-button
              >
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
    </el-card>

    <el-card>
      <stylizedTable
        title="原文档归属位置"
        :selector="false"
        :ordinal="false"
        :queryParams="queryParams"
        :loading="false"
        :total="total"
        :tableItem="tableItem"
        :tableData="ArchiveLocationList"
        :buttonList="buttonList"
        @getList="getList"
        @reset="getList"
      />
    </el-card>

    <!-- 添加或修改原文件归档位置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="128px">
        <el-form-item label="合同所属公司" prop="belongingCompanyId">
          <el-select
            v-model="form.belongingCompanyId"
            placeholder="请选择"
            style="width: 332.009px"
          >
            <el-option
              v-for="item in selectorParameter"
              :key="item.belongingCompanyId"
              :label="item.name"
              :value="item.belongingCompanyId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="原文件归档位置" prop="archiveLocation">
          <el-input
            v-model="form.archiveLocation"
            placeholder="请输入原文件归档位置"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listArchiveLocation,
  getArchiveLocation,
  delArchiveLocation,
  addArchiveLocation,
  updateArchiveLocation,
} from "@/api/contract/ArchiveLocation";
import { listBelongingCompany } from "@/api/contract/BelongingCompany";
export default {
  name: "ArchiveLocation",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 原文件归档位置表格数据
      ArchiveLocationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        belongingCompanyId: null,
        archiveLocation: null,
        state: null,
      },
      //下拉框使用参数
      selectorParameter: [],
      selectorParameterMark: 0,
      buttonList: [
        {
          icon: "el-icon-circle-plus-outline",
          type: "primary",
          text: "添加",
          methods: this.handleAdd,
        },
      ],
      tableItem: [
        {
          porp: "合同所属公司",
          data: "companyName",
        },
        {
          porp: "原文件归档位置",
          data: "archiveLocation",
        },
        {
          porp: "操作",
          solt: {
            name: 5,
            display: 0,
            operate: [
              {
                text: "修改",
                icon: "editTable",
                methods: this.handleUpdate,
                mark: "修改",
                markIcon: "editTable02",
              },
              {
                text: "停用",
                icon: "stop",
                methods: this.handleStatusChange,
                mark: "启用",
                markIcon: "stop02",
                methods02: this.handleStatusChange,
              },
            ],
          },
        },
      ],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        belongingCompanyId: [
          {
            required: true,
            message: "合同所属公司id不能为空",
            trigger: "blur",
          },
        ],
        archiveLocation: [
          {
            required: true,
            message: "原文件归档位置不能为空",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getSelectorParameterList();
  },
  methods: {
    /** 查询原文件归档位置列表 */
    getList() {
      this.loading = true;
      listArchiveLocation(this.queryParams).then((response) => {
        this.ArchiveLocationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getSelectorParameterList() {
      listBelongingCompany({ pageNum: 1, pageSize: 999 }).then((res) => {
        console.log(res);
        this.selectorParameter = res.rows;
      });
    },
    ceshi() {
      console.log("添加");
    },
    //启用/禁用方法
    handleStatusChange(row) {
      console.log(row);
      // 保存原始状态
      let originalStatus = row.state;
      let actionText = row.state === "0" ? "停用" : "启用";

      this.$confirm(`确认要${actionText}'${row.archiveLocation}'吗？`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        closeOnClickModal: false,
        type: "warning",
      })
        .then(() => {
          // 根据状态设置新的值
          let newStatus = row.state === "1" ? "0" : "1";
          console.log(row.archiveLocationId);
          console.log(newStatus);
          let body = {
            archiveLocationId: row.archiveLocationId,
            state: newStatus,
          };
          updateArchiveLocation(body).then(() => {
            this.$modal.msgSuccess("操作成功");
            this.getList(); // 成功后刷新列表
          });
        })
        .catch(() => {
          // 取消时恢复原来的状态
          row.status = originalStatus;
        });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        archiveLocationId: null,
        belongingCompanyId: null,
        archiveLocation: null,
        state: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.belongingCompanyId = null;
      this.queryParams.archiveLocation = null;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.archiveLocationId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加原文件归档位置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const archiveLocationId = row.archiveLocationId || this.ids;
      getArchiveLocation(archiveLocationId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改原文件归档位置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.archiveLocationId != null) {
            updateArchiveLocation(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addArchiveLocation(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const archiveLocationIds = row.archiveLocationId || this.ids;
      this.$modal
        .confirm(
          '是否确认删除原文件归档位置编号为"' +
            archiveLocationIds +
            '"的数据项？'
        )
        .then(function () {
          return delArchiveLocation(archiveLocationIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "contract/ArchiveLocation/export",
        {
          ...this.queryParams,
        },
        `ArchiveLocation_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style scoped>
::v-deep .el-input--medium .el-input__inner {
  height: 40px;
}

::v-deep .el-dialog {
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  margin: 0px !important;
}
::v-deep .el-dialog__header {
  background-color: rgb(248, 250, 251); /* 你可以根据需求修改颜色 */
  color: rgb(99, 86, 110); /* 标题文字颜色 */
}
</style>
